package com.java502.shirodemo502.config;

import com.java502.shirodemo502.realm.MyRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:Teacher黄
 * @date:Created at 2019/12/30
 */
@Configuration
public class ShiroConfig {


    /**
     * 加密算法
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(1);
        return matcher;
    }

    /**
     * 自定义realm
     * @return
     */
    @Bean
    public MyRealm myRealm1(){
        MyRealm myRealm = new MyRealm();
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return myRealm;
    }

    /**
     * 安全管理器
     * @return
     */
    @Bean
    public SecurityManager securityManager(){
        // web环境的安全管理器
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm1());
        // 设置记住我
        securityManager.setRememberMeManager(rememberMeManager());
        return securityManager;
    }




    /**
     * shiro的过滤器
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        // 配置拦截器
        // 配置不会被拦截的链接 顺序判断，因为前端模板采用了 thymeleaf，
        Map<String,String> filterChainDefinitionMap = new HashMap<>(16);
        // 这里不能直接使用 ("/static/**", "anon")来配置匿名访问，必须配置到每个静态目录
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/fonts/**", "anon");
        filterChainDefinitionMap.put("/img/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        //配置退出 过滤器,其中的具体的退出代码 Shiro 已经替我们实现了
        filterChainDefinitionMap.put("/logout", "logout");
        // 过滤链定义，从上向下顺序执行，一般将/**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
        // authc:所有 url 都必须认证通过才可以访问; anon:所有 url 都都可以匿名访问-->
        filterChainDefinitionMap.put("/user/login","anon");
        filterChainDefinitionMap.put("/user/login_page","anon");

        //
        filterChainDefinitionMap.put("/member/testRole","roles[学生管理]");
        filterChainDefinitionMap.put("/member/testPer","permsOr[member:testPer,member:testPer2]");

        // 其余所有的访问都需要认证(登陆才可以访问)
        filterChainDefinitionMap.put("/**", "user");


        // 如果不设置默认会自动寻找 Web 工程根目录下的"/login.jsp"页面
        shiroFilterFactoryBean.setLoginUrl("/user/login_page");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //未授权界面;
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        // ########################注入自定义的filter#################
        // 用一个map来装自定义的filter
        Map<String, Filter> filterMap = new HashMap<>(16);
        filterMap.put("permsOr",permsOr());
        // 交给shiroFilterFactoryBean
        shiroFilterFactoryBean.setFilters(filterMap);


        return shiroFilterFactoryBean;
    }



    /**
     * 自定义的filter or
     * @return
     */
    @Bean
    public PermsOrFilter permsOr(){
        return new PermsOrFilter();
    }


    /**
     * cookie对象
     * @return
     */
    @Bean
    public SimpleCookie cookie(){
        // 创建cookie对象
        SimpleCookie cookie = new SimpleCookie();
        // 设置时间
        cookie.setMaxAge(3600);
        cookie.setName("hsj");
        return cookie;
    }

    /**
     * 记住我的管理器
     * @return
     */
    @Bean
    public CookieRememberMeManager rememberMeManager(){
        CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
        // 设置cookie
        rememberMeManager.setCookie(cookie());
        // 设置加密
        rememberMeManager.setCipherKey(Base64.decode("cXdlcnQxMjNxd2VydDEyMw=="));
        return rememberMeManager;
    }





    /**
     * 管理shiro相关bean的声明周期
     * @return
     */
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    /**
     * 切面的代理
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }






}
